/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */
#include "msc_diskio.h"
#include "string.h"
#include "operate_spim.h"

/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	return 0;
}


/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	DRESULT res;
    uint32_t flash_addr;
    
	switch (pdrv) {
        case 0:  // w25q128
            {
                w25qxx_flash_read(sector*SPIM_SECTOR_SIZE, buff, count*SPIM_SECTOR_SIZE);
                res=0;
                break;
            }
        default:
            res=1;
            break;
	}
	if(res==0x00)return RES_OK;
    else return RES_ERROR;	  
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	DRESULT res;
    
    switch(pdrv)
	{
        case 0:  // w25q128
            {
                w25qxx_erase_sector(sector*SPIM_SECTOR_SIZE);
                w25qxx_flash_write(sector*SPIM_SECTOR_SIZE, buff, count*SPIM_SECTOR_SIZE);
                res=0;
                break;
            }
		default:
			res=1;
            break;
	}
	if(res == 0x00) return RES_OK;
    else return RES_ERROR;	
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
    DRESULT res;
	int result;
    
    if(pdrv == 0)
    {
        switch(cmd){
            case CTRL_SYNC:
                result = RES_OK;
                break;
            case GET_SECTOR_SIZE:
                *(DWORD*)buff = SPIM_SECTOR_SIZE;
                result = RES_OK;
                break;
            case GET_SECTOR_COUNT:
                *(DWORD*)buff = SPIM_FLASH_SIZE / SPIM_SECTOR_SIZE;
                result = RES_OK;
                break;
            case GET_BLOCK_SIZE:
                *(DWORD*)buff = 1;
                result = RES_OK;
                break;
            default:
                result = RES_PARERR;
            break;
        }
    }
	return RES_OK;
}

uint16_t date_time[6] = {2025,1,1, 00,00,00};
/*-----------------------------------------------------------------------*/
/* Get current time                                                      */
/*-----------------------------------------------------------------------*/ 
DWORD get_fattime(void)
{
    DWORD u16_dateTime;
    
	u16_dateTime = date_time[0]- 1980;
	u16_dateTime = u16_dateTime<<25;
	u16_dateTime |= date_time[1]<<21;
	u16_dateTime |= date_time[2]<<16;
	u16_dateTime |= date_time[3]<<11;
	u16_dateTime |= date_time[4]<<5;
	u16_dateTime |= date_time[5];
	
	return u16_dateTime;
}

